package com.liuanbang.saro.sarodemo.ui.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.bumptech.glide.Glide;
import com.liuanbang.saro.sarodemo.R;
import com.liuanbang.saro.sarodemo.adapter.CustSocialSecurityListViewAdapter;
import com.liuanbang.saro.sarodemo.base.BaseActivity;
import com.liuanbang.saro.sarodemo.view.MyListView;
import com.liuanbang.saro.sarodemo.view.NumberChangeTextView;
import com.liuanbang.saro.sarodemo.view.NumberFormatEnum;
import com.liuanbang.saro.sarodemo.view.ObservableScrollView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 刘安邦 on 2016/8/29.
 * Email:liuanbang@aliyun.com
 * 描述:社保管家界面
 * 修改:       2016/8/29
 */
public class CustSocialSecurityActivity extends BaseActivity implements ObservableScrollView.ScrollViewListener {

    private Context context = this;
    @Bind(R.id.convenientBanner_social_security)
    ConvenientBanner convenientBannerSocialSecurity;
    @Bind(R.id.textView_social_security_people_number)
    NumberChangeTextView textViewSocialSecurityPeopleNumber;
    @Bind(R.id.me_open_mycash_rl)
    RelativeLayout meOpenMycashRl;
    @Bind(R.id.textView_social_security_cash_reserve)
    NumberChangeTextView textViewSocialSecurityCashReserve;
    @Bind(R.id.textView_social_security_should_pay)
    NumberChangeTextView textViewSocialSecurityShouldPay;
    @Bind(R.id.me_mycoupon)
    RelativeLayout meMycoupon;
    @Bind(R.id.radioButton_social_security_pay_social_security)
    RadioButton radioButtonSocialSecurityPaySocialSecurity;
    @Bind(R.id.radioButton_protection_main_menu_policyadmin)
    RadioButton radioButtonProtectionMainMenuPolicyadmin;
    @Bind(R.id.radioButton_protection_main_menu_policyadviser)
    RadioButton radioButtonProtectionMainMenuPolicyadviser;
    @Bind(R.id.radioButton_protection_main_menu_insurance1)
    RadioButton radioButtonProtectionMainMenuInsurance1;
    @Bind(R.id.radioButton_protection_main_menu_claims)
    RadioButton radioButtonProtectionMainMenuClaims;
    @Bind(R.id.radioButton_protection_main_menu_insurance)
    RadioButton radioButtonProtectionMainMenuInsurance;
    @Bind(R.id.radioButton_protection_main_menu_policyadmin1)
    RadioButton radioButtonProtectionMainMenuPolicyadmin1;
    @Bind(R.id.radioButton_protection_main_menu_queryschedule)
    RadioButton radioButtonProtectionMainMenuQueryschedule;
    @Bind(R.id.textview_frame_protection_insurance_adviser_menu_left)
    TextView textviewFrameProtectionInsuranceAdviserMenuLeft;
    @Bind(R.id.textview_frame_protection_insurance_adviser_menu_right)
    TextView textviewFrameProtectionInsuranceAdviserMenuRight;
    @Bind(R.id.relativelayout_protection_insurance_adviser_menu_right)
    RelativeLayout relativelayoutProtectionInsuranceAdviserMenuRight;
    @Bind(R.id.listView_social_security)
    MyListView listViewSocialSecurity;
    @Bind(R.id.scrollView_social_security)
    ObservableScrollView scrollViewSocialSecurity;
    @Bind(R.id.linearlayout_social_security_head)
    LinearLayout linearlayoutSocialSecurityHead;
    @Bind(R.id.button_social_security_talk)
    Button buttonSocialSecurityTalk;
    private int[] xy = null;

    private int height = 0;
    //------------------------------------测试临时数据---------------------------------------------------------
    private String[] bitmapImageUrls = new String[]{"http://img1.imgtn.bdimg.com/it/u=3981866843,1498594554&fm=11&gp=0.jpg"
            , "http://img3.imgtn.bdimg.com/it/u=4026605029,3350468808&fm=11&gp=0.jpg",
            "http://img1.imgtn.bdimg.com/it/u=3981866843,1498594554&fm=11&gp=0.jpg",
            "http://img3.imgtn.bdimg.com/it/u=4026605029,3350468808&fm=11&gp=0.jpg"};
    private List<String> localImages = new ArrayList<>();

    private List<Object> list_data = new ArrayList<>();
    //-------------------------------------------------------------------------------------------------------

    /**
     * 广告栏初始化方法
     */
    private void initBanner() {
        //自定义你的Holder，实现更多复杂的界面，不一定是图片翻页，其他任何控件翻页亦可。
        convenientBannerSocialSecurity.setPages(
                new CBViewHolderCreator<LocalImageHolderView>() {
                    @Override
                    public LocalImageHolderView createHolder() {
                        return new LocalImageHolderView();
                    }
                }, localImages)
                //设置两个点图片作为翻页指示器，不设置则没有指示器，可以根据自己需求自行配合自己的指示器,不需要圆点指示器可用不设
                // .setPageIndicator(new int[]{R.drawable.img_social_security_round1, R.drawable.img_social_security_round2})
                //设置指示器的方向
                .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_cust_social_security);
        ButterKnife.bind(this);
        //设置标题栏的透明度
        linearlayoutSocialSecurityHead.setBackgroundColor(Color.argb(20, 0x55, 0xc1, 0xf5));

        //为ImageView添加
        for (int i = 0; i < 4; i++) {
            localImages.add(bitmapImageUrls[i]);
            list_data.add(i);
        }

       /* LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(20, 150, 0, 0);//4个参数按顺序分别是左上右下*/
        initBanner();
        //设置广告栏自动切换时间
        convenientBannerSocialSecurity.startTurning(3000);
        //设置广告栏监听事件
        convenientBannerSocialSecurity.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                showToast(position + "");
            }
        });
        convenientBannerSocialSecurity.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        listViewSocialSecurity.setFocusable(false);
        listViewSocialSecurity.setAdapter(new CustSocialSecurityListViewAdapter(list_data, R.layout.custom_social_security_listview));

        //获取顶部图片高度后，设置滚动监听
        ViewTreeObserver vto = convenientBannerSocialSecurity.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                convenientBannerSocialSecurity.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                //这里根据需求 进行了改动
                height = convenientBannerSocialSecurity.getHeight() / 2;
                convenientBannerSocialSecurity.getWidth();
                scrollViewSocialSecurity.setScrollViewListener(CustSocialSecurityActivity.this);
            }
        });
        textViewSocialSecurityPeopleNumber.start(NumberFormatEnum.FORMAT_0);
        textViewSocialSecurityCashReserve.start(NumberFormatEnum.FORMAT_2);
        textViewSocialSecurityShouldPay.start(NumberFormatEnum.FORMAT_2);
    }

    @Override
    protected void setListener() {

    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {

    }

    @Override
    public Toolbar getToolbar() {
        return null;
    }


    /**
     * 广告栏加载图片的ViewHolder
     */
    public class LocalImageHolderView implements Holder<String> {
        private ImageView imageView;

        @Override
        public View createView(Context context) {
            imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            return imageView;
        }

        @Override
        public void UpdateUI(Context context, final int position, String data) {
            Glide.with(context).load(data).into(imageView);
        }
    }

    /**
     * ScrollView 滚动监听,用于改变 标题栏透明度
     *
     * @param scrollView
     * @param x
     * @param y
     * @param oldx
     * @param oldy
     */
    @Override
    public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
        if (y <= height) {
            float scale = (float) y / height;
            float alpha = (255 * scale);

            //layout全部透明
            //layoutHead.setAlpha(scale);
            //只是layout背景透明(仿知乎滑动效果)55c1f5 Color.argb((int) alpha, 0xfd, 0x91, 0x5b)
            linearlayoutSocialSecurityHead.setBackgroundColor(Color.argb((int) alpha, 0x55, 0xc1, 0xf5));
        }
    }

}
